﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;

namespace TMS
{
    public partial class InternalDispatchNoteControl : UserControl
    {
        Product prod = new Product();
        Users users = new Users();
        Users ActiveUser = Program.GetActiveUser;
        public InternalDispatchNoteControl()
        {
            Program.GetMainForm.Text = "TMS - Интерни испратници";
            InitializeComponent();
            dgvDispatchList.DataSource = prod.GetAllInternalDispatches().Tables[0];
            dgvDispatchList.Columns[0].Visible = false;
            dgvDispatchList.Columns[1].HeaderText = "Датум";
            dgvDispatchList.Columns[2].HeaderText = "Одговорен";
            dgvDispatchList.Columns[3].HeaderText = "Испратница бр.";
            dgvDispatchList.Columns[3].DisplayIndex = 1;
            dgvDispatchList.Columns[4].HeaderText = "Статус";
            dgvDispatchList.Columns[5].HeaderText = "Од магацин";
            dgvDispatchList.Columns[6].HeaderText = "Во магацин";

            FillResponsibels();
            FillWarehouses();
            cmbWarehousesFrom.ValueMember = "name";
            cmbWarehousesTo.ValueMember = "name";
        }

        public void FillWarehouses()
        {
            MySqlDataAdapter MyDA = new MySqlDataAdapter();
            DataSet table = new DataSet();
            DataSet table1 = new DataSet();
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;
            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();
            string sqlSelectAll = "SELECT * from warehouses";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);
            MyDA.Fill(table);
            MyDA.Fill(table1);
            mysqlCon.Close();

            table.Tables[0].Rows[0].Delete();
            table1.Tables[0].Rows[0].Delete();
            cmbWarehousesFrom.DataSource = table.Tables[0];
            cmbWarehousesTo.DataSource = table1.Tables[0];


        }

        public void FillResponsibels()
        {
            DataTable table = users.GetAllUserNameAndSurname();
            foreach (DataRow row in table.Rows)
            {
                cmbResponsible.Items.Add(row.ItemArray[0].ToString());
                cmbResponsible.Items.Remove("Администратор ");
                cmbResponsible.SelectedItem = ActiveUser.GetName()+" "+ActiveUser.GetSurname();
            }
        }


        private void dgvDispatchList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Program.GetMainForm.registerInHistoryMain(ControlsEnum.CREATE_EDIT_INTERNAL_DISPATCH_NOTE, dgvDispatchList.CurrentRow);
            Program.GetMainForm.ShowControl(ControlsEnum.CREATE_EDIT_INTERNAL_DISPATCH_NOTE, dgvDispatchList.CurrentRow);
            this.Dispose();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            grNew.Visible = true;
        }

        private void ToStClose_Click(object sender, EventArgs e)
        {
            if (dgvDispatchList.SelectedRows.Count == 0)
                MessageBox.Show("Нематате селектирано испратница");
            else
            {
                if (dgvDispatchList.SelectedRows[0].Cells[4].Value.ToString() == "Затворена")
                    MessageBox.Show("Испратницата е веќе затворена");
                else
                {
                    prod.CloseInternalDispatchNote(Convert.ToInt32(dgvDispatchList.SelectedRows[0].Cells[3].Value));
                    this.Dispose();
                    Program.GetMainForm.ShowControl(ControlsEnum.INTERNAL_DISPATCHES_CONTROL);
                }
            }
        }

        private void ToStSave_Click(object sender, EventArgs e)
        {
            prod.AddInternalDispatch(prod.GetLastInternalDispatch() + 1, cmbResponsible.SelectedItem.ToString(), dtpDate.Value, "Отворена",cmbWarehousesFrom.SelectedValue.ToString(),cmbWarehousesTo.SelectedValue.ToString());
            this.Dispose();
            Program.GetMainForm.ShowControl(ControlsEnum.INTERNAL_DISPATCHES_CONTROL);
        }

        private void ToStBack_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Back())
                this.Dispose();
        }


        private void ToStForward_Click(object sender, EventArgs e)
        {
            if (Program.GetMainForm.Forward())
                this.Dispose();
        }

        
    }
}
